package cn.controller;

import cn.entities.MenuInfo;
import cn.entities.RoleInfo;
import cn.entities.UserInfo;
import cn.entities.UserRole;
import cn.service.RoleService;
import cn.service.UserService;
import cn.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @GetMapping("user/view")
    public String  toIndex(){
        return "user";
    }

    @GetMapping("/user")
    @ResponseBody
    public ResultVo<List<UserInfo>> getAllUser(){
        List<UserInfo> userList = userService.getAllUser();
        return new ResultVo<>(20000,"",userList);
    }

    @GetMapping("/roleName")
    @ResponseBody
    public ResultVo<List<RoleInfo>> getAllRole(){
        List<RoleInfo> allRoleInfo = roleService.getAllRole();
        return new ResultVo<>(2000,"查询成功",allRoleInfo);
    }

    @PostMapping("/user")
    @ResponseBody
    public ResultVo<Object> addUserInfor(UserInfo userInfo){
        userService.addUserInfo(userInfo);
        return new ResultVo<>(20000,"插入成功",null);
    }

    @GetMapping("/user/{id}")
    @ResponseBody
    public UserInfo updateUserInfor(@PathVariable("id") Integer id){
        UserInfo userInfo = userService.getUserById(id);
        System.out.println("userInfo:"+userInfo);
        return userInfo;
    }

    @DeleteMapping("/user")
    @ResponseBody
    public ResultVo<Object> delUserById(@RequestParam("ids")String ids){
        userService.deletUser(ids);
        return new ResultVo<>(20000,"删除成功",null);
    }
}
